﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
 namespace Base.Common {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：SiemensUtil                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/3/25 15:09:23                        
   *└────────────────────────────────────────────────┘
   **/
   public class BaseUtil {
        public static int int32(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }
            byte[] a = new byte[]
                  {
                            b[begin+3],
                            b[begin+2],
                            b[begin+1],
                            b[begin]
                  };
            return BitConverter.ToInt32(a ,0);
        }
        public static ushort ushort16(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }
            byte[] a = new byte[]
                  {
                            b[begin+1],
                            b[begin]
                  };
            return BitConverter.ToUInt16(a ,0);
        }

        public static ushort getByte(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }

            return b[begin];
        }
        static public byte[] toS7L2(int value) {
            byte[] b = BitConverter.GetBytes(value);
            return new byte[] { b[1] ,b[0] };
        }
        static public byte[] toS7L4(int value) {
            byte[] b = BitConverter.GetBytes(value);
            return new byte[] { b[3] ,b[2] ,b[1] ,b[0] };
        }
        public static string toJosn(Object obj) {
            if (obj == null)
            {
                return "";
            }
            String josn= JsonHelper.mySerializeObject(obj);
           return  josn.Replace("\r\n" ,"").Replace(" ","");
        }
        public static T toObj<T>(String jsonStr) {
            if (string.IsNullOrEmpty(jsonStr))
            {
                return default(T);
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonStr);
        }
        public static string MD5Encrypt(string strText) {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(strText));
            StringBuilder sBuilder = new StringBuilder();
            for( int i = 0;i < data.Length;i++ ) {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
         static char[] c = null;
        static Dictionary<char ,int> Base64Code = new Dictionary<char ,int>();
        static object obj = new object();
        static readonly long baseTicks ;
        static long last=0;
        static BaseUtil() {
          
            String key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[]abcdefghijklmnopqrstuvwxyz";
             c = key.ToCharArray();
             for( int i = 0;i < c.Length;i++ ) {
                Base64Code.Add(c[i] ,i);
            }
            baseTicks = new DateTime(2000 ,1 ,1).Ticks ;
        }
          public static string to64(long xx) {
            if( xx == 0 ) {
                return "0";
            }
            string a = "";
            while( xx >= 1 ) {
                int index = ( int ) (xx & 63);
                xx >>= 6;
                a = c[index] + a;
             }
            return a;
        }
             public static string to64() {
            long xx = (DateTime.Now.Ticks - baseTicks) >> 16;
            lock( obj ) {
                if( last >= xx ) {
                    last++;
                } else {
                    last =xx ;
                }
                xx = last;
            }
            return to64(xx) + BaseConfig.ipCode;
          }
         public static long toLong(string xx) {
            long a = 0;
            int power = xx.Length ;
            for( int i = 0;i < power;i++ ) {
                a <<= 6;
                char key = xx[i];
                a += Base64Code[key] ;
               
            }
             return a;
        }
       
      
          
       }
}
